<%
# View which displays a list of all the users under an instance,
# specified by @instance.  
#
# Author::      Eli Fox-Epstein, efoxepstein@wesleyan.edu
# Author::      Dimitar Gochev, dimitar.gochev@trincoll.edu
# Copyright::   Humanitarian FOSS Project (http://www.hfoss.org), Copyright (C) 2009.
# License::     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
%>

<% content_for :title, title('Contacts', @instance.short_name)%>
<% content_for :javascripts, javascript_include_tag('jquery','users')%>
<% content_for :body do %>

<%= flashes %>
    <%= link_to 'Download these contacts', instance_vcards_path(@instance, 'contacts', :users =>(@users.map {|u|u.id}).join(',')), :class => 'button-link' %>
    <%= mail_to (@users.map {|u| u.email}).join(';'), 'Email these contacts', :class => 'button-link', :target => '_blank' %>
	<div id="wrap">
		<% if @search or @group_filter %>
			<h1>
				<%="Results for #{h @search}" if @search %>
				<%=' in ' if @search and @group_filter %>
				<%="#{h @group_filter.name} " if @group_filter%>
				<span class="tiny">&laquo; Contacts</span></h1>
		<% else %>
			<h1>All Contacts <span class="tiny">&laquo; <%=@instance.long_name%></span></h1>
		<% end %>
		<div id="filters">
			<% form_tag instance_users_path(@instance), :method => :get, :id => 'filter-bar' do  
			    groups = @instance.groups.reject {|g| g.memberships.length == 0 }
			    %>
				<h3>Filters</h3>
				<%= select(:filters, 'groups][id', groups.map{|g| [g.name, g.id]},
					:include_blank => 'All Groups', :selected => (@group_filter && @group_filter.id)) %>
				<%= select(:filters, 'state', {'Active'=>'active', 'Pending'=>'pending_approval'}, :selected => @pending_filter) if @pending_filter%>
				<%= text_field_tag :search, (params[:search] || 'Search Keywords'), :size => 20, :class=>(!params[:search] || params[:search] == '' ? 'blank':'')%>
				<%= submit_tag "Go", :name => nil %>
			<% end %>
		</div>	
		<ul id="index">
			<li><em>Jump to last name:</em></li>
		<% ('A'..'Z').each do |l| %>
			<li><a href="#<%=l%>"><%=l%></a></li>	
		<% end %>
		</ul>
	</div>		
		<% if @users.size == 0 %>
		    <h3 class="centered">
		        Sorry, no contacts match your search or filters.
		    </h3>
		<% end %>
		<ul id="contacts">
		<% 	last = ''
			@users.each do |u|
				if u.last_name[0,1].upcase != last
					last = u.last_name[0,1].upcase
				%>
					<li class="separator" id="<%=last%>"><%=last%></li>
				<% end %>
			<li>
				<div class="name">
				    <%= link_to u.last_first, instance_user_url(@instance, u) %>
				    <span class="vcard"><%=link_to 'vCard', instance_user_url(@instance, u, :format => :vcf) -%></span></div>
				<div class="person-info">
				    <% if u.state != 'active' && u.updatable_by?(@current_user) %>
				        <span>
				            <%= link_to 'Approve', instance_user_url(@instance, u, :user => {:state => :active}), {:method => :put} %>
				        </span>
				    <% end %>
					<% if u.cell_phone? %>
						<span class="phone">
							<span class="contact-label">cell phone:</span>
							<%=phone u.cell_phone %> <%= preferred if u.preferred_is_cell? and u.desk_phone? %>
						</span>
					<% end %>
					<% if u.desk_phone? %>
						<span class="phone">
							<span class="contact-label">desk phone:</span>
							<%=phone u.desk_phone_with_ext %> <%= preferred if !u.preferred_is_cell and u.cell_phone? %>
						</span>
					<% end %>
					<% if u.email %>
						<span class="email">
							<span class="contact-label">email:</span>
							<%=mail_to u.email, u.email, :target => '_blank' %>
						</span>
					<% end %>
					<% if u.groups and u.groups.length > 0 %>
						<div class="groups-container">
							<span class="contact-label">groups:</span>
							<ul class="groups">
							<% if u.groups.length > 4 %>
								<% u.groups[0,4].each do |g| %>
								<li><%= link_to g.name, instance_group_type_group_path(@instance,g.group_type,g) %></li>
								<% end %>
								<li class="more"><%= link_to "["+(u.groups.length-4).to_s+"&nbsp;more]", instance_user_url(@instance, u)%></li>
							<% else %>
								<% u.groups.each do |g| %>
								<li><%= link_to g.name, instance_group_type_group_path(@instance,g.group_type,g) %></li>
								<% end %>
							<% end %>
							</ul>
						</div>
					<% end %>
				</div>
			</li>
		<% end %>
		</ul>
		<div style="clear:both"></div>
		<%= will_paginate @users %>
<% end %>
